package io.renren.modules.bike.controller;

import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.bike.entity.MoneyEntity;
import io.renren.modules.bike.service.MoneyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import static io.renren.common.utils.ShiroUtils.getUserId;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-03-14 12:21:35
 */
@RestController
@RequestMapping("bike/money")
public class MoneyController {
    @Autowired
    private MoneyService moneyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("bike:money:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        if (userId != 1) {
            PageUtils page = moneyService.queryPageUser(params, userId);

            return R.ok().put("page", page);
        }
        PageUtils page = moneyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("bike:money:info")
    public R info(@PathVariable("id") Integer id) {
        MoneyEntity money = moneyService.getById(id);

        return R.ok().put("money", money);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("bike:money:save")
    public R save(@RequestBody MoneyEntity money) {
        MoneyEntity amount = moneyService.selectOne(getUserId());
        float anInt = 0;
        try {
            anInt = Float.parseFloat(amount.getMoney());
        } catch (Exception e) {
            MoneyEntity entity = new MoneyEntity();
            Long userId = getUserId();
            if (entity.getUid() == null) {
                money.setUid(Math.toIntExact(userId));
            }
            entity.setMoney("0");
            moneyService.save(entity);
            anInt = anInt + Float.parseFloat(money.getMoney());
            money.setMoney(String.valueOf(anInt));
            money.setId(entity.getId());
            moneyService.updateById(money);
            return R.ok();
        }
        anInt = anInt + Float.parseFloat(money.getMoney());
        money.setMoney(String.valueOf(anInt));
        money.setId(amount.getId());
        moneyService.updateById(money);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("bike:money:update")
    public R update(@RequestBody MoneyEntity money) {
        moneyService.updateById(money);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("bike:money:delete")
    public R delete(@RequestBody Integer[] ids) {
        moneyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
